//
//  SingleCell.m
//  Kugou-OC
//
//  Created by BH on 16/5/26.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "ExpendCommonCell.h"
#import "GlobalDefine.h"
#import "CustomImageButton.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@implementation ExpendCommonCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"SingleCell";
    ExpendCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ExpendCommonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _leftBtn = ALLOC(UIButton);
        _rightBtn = ALLOC(UIButton);
        _titleLab = ALLOC(UILabel);
        _subtitleLab = ALLOC(UILabel);
        
        [self.contentView sd_addSubviews:@[_leftBtn,_rightBtn,_titleLab,_subtitleLab]];
        
        self.separatorInset = UIEdgeInsetsMake(0, 50, 0, 10);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [_leftBtn setImage:IMAGE(@"localmusic_addtoplaylist") forState:UIControlStateNormal];
    
    [_rightBtn setImage:IMAGE(@"localmusic_more") forState:UIControlStateNormal];
    
    _titleLab.font = TITLE_THIRD_FONT;
    _subtitleLab.font = TITLE_FOUR_FONT;
    _subtitleLab.textColor = TITLE_FOUR_TEXT_COLOR;
}

/**
 *  设置子视图frame
 *
 *  @param isOpen yes-展开 no-收起
 */
-(void)addSubviewWithOpen:(BOOL)isOpen{
    if (isOpen) {
        _subView = [[UIView alloc] init];
        _subView.backgroundColor = BG_COLOR;
        [self.contentView addSubview:_subView];
        _subView.sd_layout.leftSpaceToView(self.contentView,0).rightSpaceToView(self.contentView,0).topSpaceToView(self.contentView,60).heightIs(120);
        
        [self addBtnWithSubview:_subView];
        
        _leftBtn.sd_layout.leftSpaceToView(self.contentView,10).topSpaceToView(self.contentView,10).bottomSpaceToView(_subView,10).widthEqualToHeight();
        
        _rightBtn.sd_layout.rightSpaceToView(self.contentView,10).centerYEqualToView(_leftBtn).heightIs(44).widthEqualToHeight();
        
        _titleLab.sd_layout.leftSpaceToView(_leftBtn,5).topEqualToView(_leftBtn).rightSpaceToView(_rightBtn,5).heightIs(25);
        
        _subtitleLab.sd_layout.leftEqualToView(_titleLab).bottomEqualToView(_leftBtn).rightEqualToView(_titleLab).autoHeightRatio(0);
        
    }else{
        _leftBtn.sd_layout.leftSpaceToView(self.contentView,10).topSpaceToView(self.contentView,10).bottomSpaceToView(self.contentView,10).widthEqualToHeight();
        
        _rightBtn.sd_layout.rightSpaceToView(self.contentView,10).centerYEqualToView(self.contentView).heightIs(44).widthEqualToHeight();
        
        _titleLab.sd_layout.leftSpaceToView(_leftBtn,5).topEqualToView(_leftBtn).rightSpaceToView(_rightBtn,5).heightIs(25);
        
        _subtitleLab.sd_layout.leftEqualToView(_titleLab).bottomEqualToView(_leftBtn).rightEqualToView(_titleLab).autoHeightRatio(0);
    }
}

-(void)addBtnWithSubview:(UIView *)view{
    NSArray *keys = [_itemsDict allKeys];
    CustomImageButton *btn = nil;
    for (int i=0; i<keys.count; i++) {
        CustomImageButton *button = ALLOC(CustomImageButton);
        [button setTitle:[_itemsDict objectForKey:keys[i]] forState:UIControlStateNormal];
        [button setImage:IMAGE(keys[i]) forState:UIControlStateNormal];
        [view addSubview:button];
        
        if (i<5) {
            //第一行
            if (btn) {
                button.sd_layout.leftSpaceToView(btn,0).topSpaceToView(view,0).widthIs((SCREEN_WIDTH-20)/5).heightIs(view.height/2);
            }else{
                button.sd_layout.leftSpaceToView(view,10).topSpaceToView(view,0).widthIs((SCREEN_WIDTH-20)/5).heightIs(view.height/2);
            }
        }else{
            //第二行
            if (i==5) {
                button.sd_layout.leftSpaceToView(view,10).topSpaceToView(btn,0).widthIs((SCREEN_WIDTH-20)/5).heightIs(view.height/2);
            }else{
                button.sd_layout.leftSpaceToView(btn,0).topEqualToView(btn).widthIs((SCREEN_WIDTH-20)/5).heightIs(view.height/2);
            }
        }
        
        btn = button;
    }
}

@end
